package com.example.sherlockzhong.plush_v2.View;

/**
 * Created by DosonLeung on 2014/8/1.
 * website:http;//www.scauhci.org
 * description:
 * it is a customize View extended the Horizontal ScrollerView for the exact function such as:scrollBack (),detectScroll (),flyOut().
 * Note:the app must be instanced at first.
 */

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;

public class WelcomeHorizontalScrollFullView extends HorizontalScrollView {

    private int scrollDistance = 0;
    private FrameLayout schedule_bar;
    private Activity app;
    private float startX = 0;
    private float endX = 0;
    private float preX = 0;
    //private int week[] = {R.id.welcome_bar_mon,R.id.welcome_bar_tue,R.id.welcome_bar_wed,R.id.welcome_bar_thu,R.id.welcome_bar_fri,R.id.welcome_bar_sat,R.id.welcome_bar_sun};


    public WelcomeHorizontalScrollFullView(Context context) {
        super(context);
        preX = getScrollX();
    }

    public WelcomeHorizontalScrollFullView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        preX = getScrollX();
    }

    public FrameLayout getSchedule_bar() {
        return schedule_bar;
    }

    public void setSchedule_bar(FrameLayout schedule_bar) {
        this.schedule_bar = schedule_bar;

    }

    public Activity getApp() {
        return app;

    }

    public void setApp(Activity app) {
        this.app = app;
    }

    public float getStartX() {
        return startX;
    }

    public void setStartX(float startX) {
        this.startX = startX;
    }

    public float getEndX() {
        return endX;
    }

    public void setEndX(float endX) {
        this.endX = endX;
    }

    public void initTo(final int step) {
        if (step >= 0 && step < 7) {
            final WelcomeHorizontalScrollFullView self = this;
            this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    self.smoothScrollTo(self.scrollDistance * step, 0);
                }
            }, 500);
            //show(step);
            preX = getScrollX();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        TableMotionEvent(ev);
        return super.onTouchEvent(ev);
    }

    public void setScrollDistance(int scrollDistance) {
        this.scrollDistance = scrollDistance;
    }

    private void TableMotionEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                this.setStartX(ev.getX());
                break;
            case MotionEvent.ACTION_UP:
                this.setEndX(ev.getX());
                scrollBack();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            default:
                break;
        }
    }

    private void scrollBack() {
        final WelcomeHorizontalScrollFullView self = this;
        View schedule_outside = (View) self.getParent();
        int scrollX = getScrollX();
        final int step = scrollX > preX ? ((int) ((float) scrollX / (float) scrollDistance + 0.8)) : ((int) ((float) scrollX / (float) scrollDistance + 0.2));
        final int distance = self.scrollDistance * step;
        this.post(new Runnable() {
            @Override
            public void run() {
                self.smoothScrollTo(distance, 0);
            }
        });
        //show(step);
        preX = distance;
        //ScheduleBar schedule_bar = (ScheduleBar) app.findViewById(R.id.schedulefull_bar);
        //schedule_bar.show(step);
    }

    public void scrollBack(int s) {
        final int step = s;
        final WelcomeHorizontalScrollFullView self = this;
        int scrollX = getScrollX();
        final int distance = self.scrollDistance * step;
        this.post(new Runnable() {
            @Override
            public void run() {
                self.smoothScrollTo(distance, 0);
            }
        });
        preX = distance;
        //show(step);
    }

    /*
    public void show(int step){
        for(int i=0;i<7;i++){
            if(i==step){
                ImageView day = (ImageView)app.findViewById(week[i]);
                day.setVisibility(View.VISIBLE);
            }
            else{
                ImageView otherDay = (ImageView)app.findViewById(week[i]);
                otherDay.setVisibility(View.GONE);
            }
        }
    }*/
}

